package engine

import (
	"learngo/crawler/persite"
)

type ConcurrentEngine struct{
	Schedule	Schedule
	WorkerCount	int

}
type Schedule interface {
	Submit(Request)
	ConfigureMasterWorkerChan(chan Request)
}

func (e *ConcurrentEngine)Run(seed ...Request){

	in:=make(chan Request)
	out:=make(chan ParseResult)
	e.Schedule.ConfigureMasterWorkerChan(in)
	for i:=0;i<e.WorkerCount;i++ {
		createWork(in,out)
	}
	for _,r:=range seed{
		e.Schedule.Submit(r)
	}
	for{
		result:=<-out
		saveChan:=persite.ItemSaver()
		//saveChan <- result.Items

		for _,item:=range result.Items{
			saveChan <- item
		}
		for _,request:=range result.Requests{
			e.Schedule.Submit(request)
		}
	}
}
func createWork(in chan Request,out chan ParseResult){
	go func() {
		for  {
			request:=<-in
			result,err:=Worker(request)
			if err!=nil {
				continue
			}
			out<-result
		}
	}()
}
